#!/usr/bin/python
from supporters.models import Supporter, PaymentCsvFile

import csv
import re
import sys
import datetime
import htmlentitydefs
import codecs

supporters_csv = csv.reader(file('../in'))
supporter_columns = supporters_csv.next()

column_name_changes = {"dateJoined": "date_joined", 
                       "dateClosed": "date_closed",
                       'firstname': 'first_name',
                       'memberid': 'member_id',
                       
                       }
date_columns = ["updated", 'date_joined', 'date_closed']
int_columns = ["memberid", "authcode"]

entitydefs = htmlentitydefs.entitydefs
entitydefs['#39'] = "'"
entitydefs['#128'] = "C"

class HTMLCodec(codecs.Codec):
    def decode(self, string):
        while True:
            match = re.search("&(.+?);", string)
            if match is None: 
                break
            char = entitydefs[match.group(1)]
            string = string.replace(match.group(0), char)
        return unicode(string, 'latin1')

htmlcodec = HTMLCodec()

for row in supporters_csv:
    sup = Supporter()
    for (k,v) in zip(supporter_columns, row):
        v = htmlcodec.decode(v)
        
        if k in column_name_changes: 
            k = column_name_changes[k]
        if k in int_columns:
            v = int(v)
        if k in date_columns:
            print "v is",k, v
            if v != '':
                v = [int(x.rstrip('-')) for x in  v.split()[0].split('-')]
            
                v = datetime.date(day=v[2], month=v[1], year=v[0])
            
        sup.__dict__[k] = v
    sup.date_joined = sup.updated
    sup.date_closed = None
    #print sup
    #dprint sup.__dict__
    sup.status = 'U'
    sup.member_type = 'U'
    sup.payment_frequency = 'U'
    sup.payment_source = 'U'
    sup.save()
    sup.payment_id = str(sup.surname).upper() + sup.member_id
    sup.save()

